
package com.probiz.estoresa.system.web.action.shipping;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;

import com.probiz.estore.common.model.system.ShippingRate;
import com.probiz.estore.core.controller.GenericStoreAdminController;
import com.probiz.estore.system.service.ShippingMethodManager;
import com.probiz.estore.system.service.ShippingRateManager;
import com.probiz.estore.webapp.util.RequestUtil;

public class ShippingRateFormController extends
		GenericStoreAdminController<ShippingRate> {
	private ShippingRateManager		shippingRateManager		= null;
	private ShippingMethodManager	shippingMethodManager	= null;
	private static final String		ShippingRate_EDIT_KEY	= "shippingRate";
	private static final String		ShippingMethod_LIST		= "shippingMethodList";

	public void setShippingRateManager(ShippingRateManager shippingRateManager) {
		this.shippingRateManager = shippingRateManager;
	}

	@Override
	protected String getEntityName(ShippingRate entity) {
		return entity.getFirstKeyColumnName();

	}


	@Override
	protected void initController() throws Exception {
		mgr = shippingRateManager;

	}

	@Override
	protected void preSave(HttpServletRequest request, ShippingRate entity,
			BindException errors) {

	}
	

	@Override
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String shippingRateId = request.getParameter("shippingRateId");
		String baseOn = request.getParameter("baseOn");
		ShippingRate shippingRate = null;
		if (StringUtils.isNotEmpty(shippingRateId)){
			Integer pkey = (new Integer(shippingRateId));
			shippingRate = this.shippingRateManager.getById(pkey);
		}else{
			shippingRate = new ShippingRate();
		}
		BigDecimal basePrice = RequestUtil.getParameterNullSafe(request,"basePrice")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"basePrice"))):BigDecimal.ZERO;
        BigDecimal maxWeight =  RequestUtil.getParameterNullSafe(request,"maxWeight")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"maxWeight"))):BigDecimal.ZERO;
        BigDecimal maxVolume = RequestUtil.getParameterNullSafe(request,"maxVolume")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"maxVolume"))):BigDecimal.ZERO;
        BigDecimal baseWeight= RequestUtil.getParameterNullSafe(request,"baseWeight")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"baseWeight"))):BigDecimal.ZERO;
        BigDecimal baseVolume= RequestUtil.getParameterNullSafe(request,"baseVolume")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"baseVolume"))):BigDecimal.ZERO;
        BigDecimal weightPerRate= RequestUtil.getParameterNullSafe(request,"weightPerRate")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"weightPerRate"))):BigDecimal.ZERO;
        BigDecimal volumePerRate= RequestUtil.getParameterNullSafe(request,"volumePerRate")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"volumePerRate"))):BigDecimal.ZERO;
        BigDecimal volumePerFee= RequestUtil.getParameterNullSafe(request,"volumePerFee")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"volumePerFee"))):BigDecimal.ZERO;
        BigDecimal itemPerRate= RequestUtil.getParameterNullSafe(request,"itemPerRate")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"itemPerRate"))):BigDecimal.ZERO;
        BigDecimal increaseUnit= RequestUtil.getParameterNullSafe(request,"increaseUnit")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"increaseUnit"))):BigDecimal.ZERO;
        BigDecimal minWeight= RequestUtil.getParameterNullSafe(request,"minWeight")!=""?(new BigDecimal(RequestUtil.getParameterNullSafe(request,"minWeight"))):BigDecimal.ZERO;
        try{ 
        	if(basePrice!=null){
        	 basePrice= basePrice.setScale(2,BigDecimal.ROUND_HALF_UP);
        	 shippingRate.setBasePrice(basePrice);
        	}
        	if(maxWeight!=null){
        		maxWeight= maxWeight.setScale(2,BigDecimal.ROUND_HALF_UP);
           	 shippingRate.setMaxWeight(maxWeight);
           	}
        	if(maxVolume!=null){
        		maxVolume= maxVolume.setScale(2,BigDecimal.ROUND_HALF_UP);
           	 shippingRate.setMaxVolume(maxVolume);
           	}
        	if(baseWeight!=null){
        		baseWeight= baseWeight.setScale(2,BigDecimal.ROUND_HALF_UP);
           	 shippingRate.setBaseWeight(baseWeight);
           	}
        	if(baseVolume!=null){
        		baseVolume= baseVolume.setScale(2,BigDecimal.ROUND_HALF_UP);
           	 shippingRate.setBaseVolume(baseVolume);
           	}
        	if(weightPerRate!=null){
        		weightPerRate= weightPerRate.setScale(2,BigDecimal.ROUND_HALF_UP);
           	 shippingRate.setWeightPerRate(weightPerRate);
           	}
        	if(volumePerRate!=null){
        		volumePerRate= volumePerRate.setScale(2,BigDecimal.ROUND_HALF_UP);
           	 shippingRate.setVolumePerRate(volumePerRate);
           	}
        	if(volumePerFee!=null){
        		volumePerFee= volumePerFee.setScale(2,BigDecimal.ROUND_HALF_UP);
           	 shippingRate.setVolumePerFee(volumePerFee);
           	}
        	if(itemPerRate!=null){
        		itemPerRate= itemPerRate.setScale(2,BigDecimal.ROUND_HALF_UP);
           	 shippingRate.setItemPerRate(itemPerRate);
           	}
        	if(increaseUnit!=null){
        		increaseUnit= increaseUnit.setScale(2,BigDecimal.ROUND_HALF_UP);
           	 shippingRate.setIncreaseUnit(increaseUnit);
           	}
        	if(minWeight!=null){
        		minWeight= minWeight.setScale(2,BigDecimal.ROUND_HALF_UP);
           	    shippingRate.setMinWeight(minWeight);
           	}
        	
        	
        	
        
        
        }catch(Exception e){
        	e.printStackTrace();
        }   
        shippingRate.setBaseOn(new Integer(baseOn));
        shippingRate.setDescription(RequestUtil.getParameterNullSafe(request,"description"));
        shippingRate.setShippingMethodId(new Integer(RequestUtil.getParameterNullSafe(request,"shippingMethodId")));
        shippingRate.setMetricUnitCode(RequestUtil.getParameterNullSafe(request,"metricUnitCode"));
        shippingRate.setRegionId(new Integer(RequestUtil.getParameterNullSafe(request,"regionId")));
        
        
        if (request.getParameter("delete") != null) {
    		Integer pkey = shippingRate.getShippingRateId();
            mgr.deleteById(pkey);	            
        } else {	        	
        	mgr.save(shippingRate);
            
        }
        return null;
	}

	public ModelAndView preShippingRateAction(HttpServletRequest request,HttpServletResponse response)	throws Exception	 {
		
		ModelAndView mv=new ModelAndView("system/shipping/shippingRateForm");
		String shippingRateId = request.getParameter("shippingRateId");
		ShippingRate shippingRate = null;
		if (StringUtils.isNotEmpty(shippingRateId)) {
			shippingRate = this.shippingRateManager.getById(Integer.valueOf(shippingRateId));
		}else{
			shippingRate = new ShippingRate();
			String shippingMethodId = request.getParameter("shippingMethodId");
			shippingRate.setShippingMethod(shippingMethodManager.getById(Integer.valueOf(shippingMethodId)));
		}
		// for unit tests - so we can grab an object to save
		// request.setAttribute(MetricUnit_LIST,muMgr.getMetricUnitsByType(MetricUnitManager.WEIGHT_UNIT));
		request.setAttribute(ShippingRate_EDIT_KEY, shippingRate);
		return mv;
	}

	public void setShippingMethodManager(ShippingMethodManager shippingMethodManager) {
		this.shippingMethodManager = shippingMethodManager;
	}

}